import config from '@/config/index.js'
// 在前面添加合法域名
export function frontAddHttp(value) {
	if (!value) {
		return ''
	}
	return value.toString().startsWith('http') ? value : config.baseUrl + value
}

// 富文本 图片处理
export function imgDeal(value) {
	if (!value) {
		return ''
	}
	return value.replace(/<img/g, "<img style='display:block;width:100%;height:auto;'")
}

/* 对手机号进行保密
	front_num:前面预留数量
	after_num:后面预留数量
*/
export function phoneProtect(value, front_num = 3, after_num = 4) {
	if (!value) {
		return ''
	}

	value = typeof value == 'string' ? value : value.toString()

	if (value.length > (front_num + after_num)) {
		let front = value.substring(0, front_num)
		let xing = hideText(value.substr(front_num, value.length - (front_num + after_num)))
		let after = value.substring((value.length - after_num))
		return front + xing + after
	} else {
		console.error(`请输入大于${front_num+after_num}的字符串`);
		return false
	}
}

// 手机号 3-4-4 展示
export function phoneRung(value) {
	if (!value) {
		return ''
	}
	let phone = value.toString().replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1-$2-$3')
	return phone
}


// 用星号替换文本
export function hideText(value, flag = true, xing = "*") {
	if (!value) {
		return ''
	}
	if (flag) {
		value = typeof value == 'string' ? value : value.toString()
		let xing_num = ''
		for (let i = 0; i < value.length; i++) {
			xing_num += xing
		}
		return xing_num
	} else {
		return value
	}
}


// 字符串 替换逗号
export function strReplaceComma(value, comma) {
	if (!value) {
		return ''
	}

	let newValue = value.split(',')

	return newValue.join(comma)
}


// 对第一个字符转换为大写
export function uppercaseFirst(string) {
	return string.charAt(0).toUpperCase() + string.slice(1)
}

// 使用逗号格式化数字  每3位加一,
export function toThousandFilter(num) {
	return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}



/**
 * @param {number} time
 */
export function timeAgo(time) {
	const between = Date.now() / 1000 - Number(time)
	if (between < 3600) {
		return pluralize(~~(between / 60), ' minute')
	} else if (between < 86400) {
		return pluralize(~~(between / 3600), ' hour')
	} else {
		return pluralize(~~(between / 86400), ' day')
	}
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
	const si = [{
			value: 1E18,
			symbol: 'E'
		},
		{
			value: 1E15,
			symbol: 'P'
		},
		{
			value: 1E12,
			symbol: 'T'
		},
		{
			value: 1E9,
			symbol: 'G'
		},
		{
			value: 1E6,
			symbol: 'M'
		},
		{
			value: 1E3,
			symbol: 'k'
		}
	]
	for (let i = 0; i < si.length; i++) {
		if (num >= si[i].value) {
			return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
		}
	}
	return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */



// 时间戳格式化
export function parseTime(time, cFormat) {

	if (!time) {
		return ''
	}
	if (arguments.length === 0) {
		return null
	}

	if ((time + '').length === 10) {
		time = +time * 1000
	}

	const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		date = new Date(parseInt(time))
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key]
		if (key === 'a') {
			return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
		}
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return timeStr
}

export function formatTime(time, option) {
	time = +time * 1000
	const d = new Date(time)
	const now = Date.now()

	const diff = (now - d) / 1000

	if (diff < 30) {
		return '刚刚'
	} else if (diff < 3600) {
		// less 1 hour
		return Math.ceil(diff / 60) + '分钟前'
	} else if (diff < 3600 * 24) {
		return Math.ceil(diff / 3600) + '小时前'
	} else if (diff < 3600 * 24 * 2) {
		return '1天前'
	}
	if (option) {
		return parseTime(time, option)
	} else {
		return (
			d.getMonth() +
			1 +
			'月' +
			d.getDate() +
			'日' +
			d.getHours() +
			'时' +
			d.getMinutes() +
			'分'
		)
	}
}


/* 数字 格式化 */
export function nFormatter(num, digits) {
	const si = [{
			value: 1e18,
			symbol: 'E'
		},
		{
			value: 1e15,
			symbol: 'P'
		},
		{
			value: 1e12,
			symbol: 'T'
		},
		{
			value: 1e9,
			symbol: 'G'
		},
		{
			value: 1e6,
			symbol: 'M'
		},
		{
			value: 1e3,
			symbol: 'k'
		}
	]
	for (let i = 0; i < si.length; i++) {
		if (num >= si[i].value) {
			return (
				(num / si[i].value + 0.1)
				.toFixed(digits)
				.replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
			)
		}
	}
	return num.toString()
}

export function html2Text(val) {
	const div = document.createElement('div')
	div.innerHTML = val
	return div.textContent || div.innerText
}

export function toThousandslsFilter(num) {
	return (+num || 0)
		.toString()
		.replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}







// 上传图片格式控制
export function updatedImg(file, obj, callback, func) {
	if (file.size < 10100000) {
		var fileName = file.name
		var suffix = fileName
			.substring(fileName.lastIndexOf('.') + 1)
			.toUpperCase()
		if (
			suffix === 'PDF' ||
			suffix === 'JPG' ||
			suffix === 'JPEG' ||
			suffix === 'PNG' ||
			suffix === 'GIF'
		) {
			return true
		} else {
			var tipType = '文件类型不正确,请重新上传'
			callback(tipType)
			return false
		}
	} else {
		var tipSize = '文件大小超过5M,请重新上传'
		callback(tipSize)
		return false
	}
}
// 上传文档格式控制
export function updatedFile(file, obj, callback, func) {
	if (file.size < 10100000) {
		var fileName = file.name
		var suffix = fileName
			.substring(fileName.lastIndexOf('.') + 1)
			.toUpperCase()
		if (
			suffix === 'DOC' ||
			suffix === 'DOCX' ||
			suffix === 'XLS' ||
			suffix === 'XLSX' ||
			suffix === 'PDF' ||
			suffix === 'ZIP' ||
			suffix === 'RAR'
		) {
			return true
		} else {
			var tipType = '文件类型不正确,请重新上传'
			callback(tipType)
			return false
		}
	} else {
		var tipSize = '文件大小超过5M,请重新上传'
		callback(tipSize)
		return false
	}
}
export function importFile(file, obj, callback, func) {
	if (file.size < 10100000) {
		var fileName = file.name
		var suffix = fileName
			.substring(fileName.lastIndexOf('.') + 1)
			.toUpperCase()
		if (
			suffix === 'XLS' ||
			suffix === 'XLSX'
		) {
			return true
		} else {
			var tipType = '文件类型不正确,请重新上传'
			callback(tipType)
			return false
		}
	} else {
		var tipSize = '文件大小超过10M,请重新上传'
		callback(tipSize)
		return false
	}
}

export function getBlob(response) {
	const blob = new Blob([response.data], {
		type: 'application/vnd.ms-excel'
	})
	const link = document.createElement('a')
	link.href = window.URL.createObjectURL(blob)
	var filename = decodeURI(response.headers.filename)
	// link.download = filename + '.xls'
	link.download = filename
	link.click()
}

// 图片 blob 流转化为可用 src
export function imgHandle(obj) {
	return window.URL.createObjectURL(obj)
}